const ZkServiceRegistry = require('./ZkServiceRegistry');
const NacosServiceRegistry = require('./NacosServiceRegistry');
const ConsulServiceRegistry = require('./ConsulServiceRegistry');

//注册中心枚举
const RegistryProvider = Object.freeze({
    ZOOKEEPER: "zookeeper",
    CONSUL: "consul",
    NACOS: "nacos",
});

class ServiceRegistryFactory {

    serviceRegistry;

    constructor(param) {
        // 初始化 注册中心
        console.debug("初始化 注册中心 工厂")
        if (RegistryProvider.ZOOKEEPER == param.provider) {
            this.serviceRegistry = new ZkServiceRegistry(param);
        } else if (RegistryProvider.NACOS == param.provider) {
            this.serviceRegistry = new NacosServiceRegistry(param);
        } else if (RegistryProvider.CONSUL == param.provider) {
            this.serviceRegistry = new ConsulServiceRegistry(param);
        } else {
            throw new Error(`没有实现 ${param.provider} 的服务发现`)
        }

        this.serviceRegistry.subscribeEvent(param.services);
    }

    /**
     * 根据provider参数搜索实现类并创建指定客户端实例，如果没有找到匹配provider的实现类将抛出异常
     * 
     * @returns 微服务注册和配置中心的客户端实例对象
     */
    getRegistry() {
        return this.serviceRegistry;
    }

}

module.exports = ServiceRegistryFactory;